import { S, U, getRequestId } from '../../lib/backend'
import Q from 'q'
import axios from 'axios'
import env from '../../config/common'
import N from '../../lib/notification'
import { UPDATE_USER } from '../mutation-types'
const localStorage = require('store')

export default {
  incrementAsync ({ commit }) {
    setTimeout(() => {
      commit('INCREMENT')
    }, 200)
  },
  // 登录
  login ({ commit }, info) {
    const params = S({
      trace: { requestId: getRequestId() },
      body: info
    })
    return Q.promise((resolve, error) => {
      axios.post(
        env.CRM_API_HOST + '/login',
        params
      ).then((response) => {
        if (response.data.errcode === 0) {
          // N.notify('success', response.data.errmsg)
          localStorage.set('user', response.data.results)
          commit(UPDATE_USER, response.data.results)
          resolve(response.data)
        } else {
          N.notify('error', response.data.errmsg)
          error(response.data)
        }
      }).catch((e) => {
        console.log(e.message)
        const msg = '系统错误...请联系管理员...'
        N.notify('error', msg)
        error({
          errcode: -1,
          errmsg: msg,
          data: []
        })
      })
    })
  },
  sendSmsCode ({ commit }, info) {
    const params = S(info)
    return Q.promise((resolve, error) => {
      axios.post(
        env.CRM_API_HOST + '/send_smscode',
        params
      ).then((response) => {
        if (response.data.errcode === 0) {
          N.notify('success', '发送验证码成功！')
          resolve(response.data)
        } else {
          N.notify('error', response.data.errmsg)
          error(response.data)
        }
      }).catch((e) => {
        console.log(e.message)
        const msg = '系统错误...请联系管理员...'
        N.notify('error', msg)
        error({
          errcode: -1,
          errmsg: msg,
          data: []
        })
      })
    })
  },
  // 登出
  logout ({ commit, rootState, state }, info) {
    const params = S({
      trace: { requestId: getRequestId() },
      body: info
    })
    return Q.promise((resolve, error) => {
      axios.get(
        env.CRM_API_HOST + '/users/' + state.user.token + '/logout',
        params
      ).then((response) => {
        if (response.data.errcode === 0) {
          if (info !== false) N.notify('success', response.data.errmsg)
          localStorage.remove('user')
          commit(UPDATE_USER, null)
          resolve(response.data)
        } else {
          if (info !== false) N.notify('error', response.data.errmsg)
          error(response.data)
        }
      }).catch((error) => {
        const msg = '系统错误...请联系管理员...'
        N.notify('error', msg)
        error({
          errcode: -1,
          errmsg: msg,
          data: []
        })
      })
    })
  },

  resetPassword ({ commit, rootState, state }, info) {
    return Q.promise((resolve, error) => {
      axios.post(
        env.CRM_API_HOST + '/admin/' + state.user.token + '/reset_password',
        info
      ).then((response) => {
        if (response.data.errcode === 0) {
          if (info !== false) N.notify('success', response.data.results.msg)
          localStorage.remove('user')
          commit(UPDATE_USER, null)
          resolve(response.data)
        } else {
          if (info !== false) N.notify('error', response.data.errmsg)
          error(response.data)
        }
      }).catch((error) => {
        const msg = '系统错误...请联系管理员...'
        N.notify('error', msg)
        error({
          errcode: -1,
          errmsg: msg,
          data: []
        })
      })
    })
  }
}
